function [ori, pre, err] = modelfortrend(data_trend, num_trend_buchang, num_train_set, jieshuq, ceshiq)
    num_train = num_train_set * num_trend_buchang - 2;%训练集个数
    
    data_trend = data_trend';
    Yt=[0,diff(data_trend,1)];
    L=diff(data_trend,2);%全体，比原始数据少2个，因为做了差分 
    Y=L(1:num_train); %输入
    a=length(L)-length(Y);%单步预测步数
    
    
    %处理的算法 : (data - 期望)/方差
    Ux=sum(Y)/num_train  ;                         % 求序列均值 
    yt=Y-Ux;
    b=0;
    for i=1:num_train
        b=yt(i)^2/num_train+b;
    end
    Y=zscore(Y);
     
    R0=0;
    for i=1:num_train
         R0=Y(i)^2/num_train+R0;   %标准化处理后的数据的方差
    end
     
    for k=1:ceshiq
        %R  协方差   
        R(k)=0;
        for i=k+1:num_train
            R(k)=Y(i)*Y(i-k)/num_train+R(k);
        end
    end
    x=R/R0;               %自相关系数x = 协方差/方差
    
    X1=x(1);xx(1,1)=1;X(1,1)=x(1);B(1,1)=x(1);
    K=0;
    T=X1;
    for t=2:num_train
        at=Y(t)-T(1)*Y(t-1);
        K=(at)^2+K;
    end                         
    U(1)=K/(num_train-1);                      % 1阶模型残差方差            
       
    for i =1:ceshiq-1
        B(i+1,1)=x(i+1);
        xx(1,i+1)=x(i);
        A=toeplitz(xx);
        XX=A\B;     %x=a\b是方程a*x =b的解
        XXX=XX(i+1);
        X(1,i+1)=XXX;
         
        K=0;T=XX;
            for t=i+2:num_train                                                       
               r=0;  
               for j=1:i+1 
                   r=T(j)*Y(t-j)+r; 
               end 
               at= Y(t)-r; 
               K=(at)^2+K;  
            end 
        U(i+1)=K/(num_train-i+1); %计算20阶以内的模型残差方差
    end
    
    q=ceshiq;
    S(1,1)=R0;
    for i = 1:q-1
        S(1,i+1)=R(i);
    end
    G=toeplitz(S);
    W=inv(G)*[R(1:q)]';                  % 参数W(i) 与X5相同  G*W = [R(1:5)]'
    
    q=jieshuq;%确定阶数
    C=0;K=0;
    for t=q+2:num_train
        at=Y(t)+Y(q+1);
        for i=1:q
            at=-W(i)*Y(t-i)-W(i)*Y(q-i+1)+at;
        end
        
        at1=Y(t-1);
        for i=1:q
            at1=-W(i)*Y(t-i-1)+at1;
        end
        C=at*at1+C;
        K=(at)^2+K;
    end
    p=C/K;
    
    XT=[L(num_train-q+1:num_train+a)];
    for t=q+1:q+a
        m(t)=0;
        for i=1:q
            m(t)=W(i)*XT(t-i)+m(t);
        end
    end
    
    m=m(q+1:q+a);
    
    for i =1:a
        m(i)=Yt(num_train+i+1)+m(i); %一次反差分
        pre(i)=data_trend(num_train+i+1)+m(i);%二次反差分 
    end
    
    for t=q+1:num_train 
        r=0;  
        for i=1:q 
           r=W(i)*Y(t-i)+r; 
        end 
        at= Y(t)-r;     
    end  
    
    
    % for t=q+1:num_train 
    %     r=0;  
    %     for i=1:q 
    %         r=W(i)*Y(t-i)+r; 
    %     end 
    %     at= Y(t)-r;     
    % end  
     
    for t=q+1:num_train 
        y(t)=0; 
        for i=1:q 
          y(t)=W(i)*Y(t-i)+y(t);   
        end 
        % y(t)=y(t)+at; 
        y(t)=Yt(t+1)-y(t); 
        y(t)=data_trend(t+1)-y(t); %反差分的过程
    end
    % figure;
    % plot(y,'r.');                    % 样本数据模型逼近曲线 
    % hold on; 
    % plot(num_train+2:num_train+a+1,pre,'r-*');  %向后a布预测
    % hold on; 
    % plot(data_trend,"--");                     % 原样本曲线 
    % title('AR(q)模型样本逼近预测曲线'); 
    % legend("训练样本预测值","测试集预测值","真实值","Location","best");
    
    ori=data_trend(num_train+2:end-1);
    [~] = calc_error(pre,ori);
    % figure; 
    % plot(1:a,ori,'-+')                      
    % hold on; 
    % plot(pre,'r-*'); 
    % title('单步，向后a步预测值和实际值对比图'); 
    % legend("真实值","预测值","Location","best");
    % hold off;

    err = data_trend(1:num_train)' - y';
    err(num_train+1) = 0;
    err(num_train+2) = 0;
end
